home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / chmod.c < prev    next >
C/C++ Source or Header  |  1994-09-30  |  6KB  |  217 lines

  1. RCS_ID_C="$Id: chmod.c,v 4.2 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      chmod.c - chmod() for usergroup link library
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  *
  9.  *      This function is based on SetOwner37 code
  10.  *      Copyright © 1993 by Geert Uytterhoeven
  11.  */
  12.  
  13. /****** net.lib/chmod *********************************************************
  14.  
  15.     NAME
  16.         chmod, fchmod - change mode of file
  17.  
  18.     SYNOPSIS
  19.         #include <sys/stat.h>
  20.  
  21.         int chmod(const char *path, mode_t mode);
  22.  
  23.         int fchmod(int fd, mode_t mode);
  24.  
  25.     DESCRIPTION
  26.         The function chmod() sets the file permission bits of the file
  27.         specified by the pathname path to mode. Fchmod() sets the permission
  28.         bits of the specified file descriptor fd. Chmod() verifies that the
  29.         process owner (user) either owns the file specified by path (or fd),
  30.         or is the super-user.  A mode is created from or'd permission bit
  31.         masks defined in <sys/stat.h>:
  32.  
  33.               #define S_IRWXU 0000700    \* RWX mask for owner *\
  34.               #define S_IRUSR 0000400    \* R for owner *\
  35.               #define S_IWUSR 0000200    \* W for owner *\
  36.               #define S_IXUSR 0000100    \* X for owner *\
  37.  
  38.               #define S_IRWXG 0000070    \* RWX mask for group *\
  39.               #define S_IRGRP 0000040    \* R for group *\
  40.               #define S_IWGRP 0000020    \* W for group *\
  41.               #define S_IXGRP 0000010    \* X for group *\
  42.  
  43.               #define S_IRWXO 0000007    \* RWX mask for other *\
  44.               #define S_IROTH 0000004    \* R for other *\
  45.               #define S_IWOTH 0000002    \* W for other *\
  46.               #define S_IXOTH 0000001    \* X for other *\
  47.  
  48.               #define S_ISUID 0004000    \* set user id on execution *\
  49.               #define S_ISGID 0002000    \* set group id on execution *\
  50.               #define S_ISVTX 0001000    \* save swapped text even after use *\
  51.  
  52.         The ISVTX (the sticky bit) indicates to the system which executable
  53.         files are shareable (pure).
  54.  
  55.         Writing or changing the owner of a file turns off the set-user-id
  56.         and set-group-id bits unless the user is the super-user.  This makes
  57.         the system somewhat more secure by protecting set-user-id
  58.         (set-group-id) files from remaining set-user-id (set-group-id) if
  59.         they are modified.
  60.  
  61.     RETURN VALUES
  62.         Upon successful completion, a value of 0 is returned.  Otherwise, a
  63.         value of -1 is returned and errno is set to indicate the error.
  64.  
  65.     ERRORS
  66.         Chmod() will fail and the file mode will be unchanged if:
  67.  
  68.         [ENOTDIR]     A component of the path prefix is not a directory.
  69.  
  70.         [ENAMETOOLONG]
  71.                       A component of a pathname exceeded 255 characters, or
  72.                       an entire path name exceeded 1023 characters.
  73.  
  74.         [ENOENT]      The named file does not exist.
  75.  
  76.         [EACCES]      Search permission is denied for a component of the
  77.                       path prefix.
  78.  
  79.         [EPERM]       The effective user ID does not match the owner of the
  80.                       file and the effective user ID is not the super-user.
  81.  
  82.         [EROFS]       The named file resides on a read-only file system.
  83.  
  84.         [EFAULT]      Path points outside the process's allocated address
  85.                       space.
  86.  
  87.         [EIO]         An I/O error occurred while reading from or writing to
  88.                       the file system.
  89.  
  90.         Fchmod() will fail if:
  91.  
  92.         [EBADF]       The descriptor is not valid.
  93.  
  94.         [EINVAL]      Fd refers to a socket, not to a file.
  95.  
  96.         [EROFS]       The file resides on a read-only file system.
  97.  
  98.         [EIO]         An I/O error occurred while reading from or writing to
  99.                       the file system.
  100.  
  101.     NOTES
  102.         This call is provided for Unix compatibility.  It does not know all
  103.         Amiga protection bits (Delete, Archive, Script).  The archive and
  104.         script bits are cleared, Delete set according the Write bit.
  105.  
  106.     SEE ALSO
  107.         open(),  chown(),  stat()
  108.  
  109. *****************************************************************************
  110. */
  111.  
  112. #include <libraries/usergroup.h>
  113. #include <proto/dos.h>
  114.  
  115. #include <sys/types.h>
  116. #include <sys/stat.h>
  117.  
  118. #include "fibex.h"
  119. #include "netlib.h"
  120.  
  121. /*
  122.  * rwx -> rwed
  123.  */
  124. const static BYTE pbits[8] = 
  125.   0, 0X2, 0X5, 0X7, 0X8, 0XA, 0XD, 0XF, 
  126. };
  127.  
  128. int chmod(const char *path, int mode)
  129. {
  130.   LONG prot = 
  131.     (pbits[mode & 7] << FIBB_OTR_DELETE) |
  132.       (pbits[(mode >> 3) & 7] << FIBB_GRP_DELETE) |
  133.     (pbits[(mode >> 6) & 7] ^ 0xf);
  134.  
  135.   if (mode & S_ISVTX)
  136.     prot |= FIBF_PURE;
  137.   if (mode & S_ISUID)
  138.     prot |= FIBF_SUID;
  139.   if (mode & S_ISGID)
  140.     prot |= FIBF_SGID;
  141.  
  142.   if (!SetProtection((STRPTR)path, prot)) {
  143.     set_errno(IoErr());
  144.     return -1;
  145.   } else {
  146.     return 0;
  147.   }
  148. }
  149.  
  150. #ifdef DEBUGGING
  151. #include <proto/usergroup.h>
  152. #include <stdlib.h>
  153. #include <string.h>
  154. #include <exec/execbase.h>
  155. #include <ctype.h>
  156.  
  157. int errno;
  158. extern struct ExecBase *SysBase;
  159.  
  160. void PrintUserFault(LONG code, const UBYTE *banner);
  161.  
  162. const static char usage[] = "usage: chmod [-fR] mode file ...";
  163.  
  164. void main(int argc, char *argv[])
  165. {
  166.   struct Process *p = (struct Process *)SysBase->ThisTask;
  167.   BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
  168.  
  169.   short perrors = 1, recursive = 0;
  170.   char *modenum; mode_t mode;
  171.  
  172.   while (argc > 1 && argv[1][0] == '-') {
  173.     switch (argv[1][1]) {
  174.     case 'f':
  175.       perrors = 0;
  176.       break;
  177.     case 'R':
  178.       recursive = 1;
  179.       break;
  180.     default:
  181.       FPrintf(Stderr, usage);
  182.       exit(10);
  183.     }
  184.     argv++, argc--;
  185.   }
  186.  
  187.   if (argc <= 2) {
  188.     FPrintf(Stderr, usage);
  189.     exit(10);
  190.   }
  191.  
  192.   modenum = argv[1]; argv++, argc--;
  193.  
  194.   mode = 0;
  195.   while (isdigit(*modenum)) {
  196.     mode <<= 3;
  197.     mode += *modenum++ - '0';
  198.   }
  199.  
  200.   if (*modenum) {
  201.     FPrintf(Stderr, usage);
  202.     exit(10);
  203.   }
  204.  
  205.   while (argc-- > 1) {
  206.     if (chmod(argv++[1], mode) == -1) {
  207.       if (perrors)
  208.     PrintUserFault(errno, "chmod");
  209.       exit(RETURN_ERROR);
  210.     }
  211.   }
  212.  
  213.   exit(0);
  214. }
  215. #endif
  216.